package lambda;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Created with IntelliJ IDEA.
 * 类名：MainTest
 * 开发人员: CoderJu
 * 创建时间: 2019/3/4 11:18
 * 描述:
 * 版本：V1.0
 */
public class MainTestForLambda  {
    public static void main(String[] args) {

         OldRunnable();
         newRunnable();
         iteratorTest();
         mapTest();
         ThreadTest();
         mapReduceTest();
         filterTest();
         predicateTest();
    }

    /**
     * 替换静态内部类
     */
    public static void OldRunnable(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("old runnable !");
            }
        }).start();
    }

    public static void newRunnable(){
        new Thread(()->System.out.println( "New Runnable , name is "+Thread.currentThread().getName())).start();
    }

    /**
     * 遍历集合
     */
    public static void iteratorTest(){
        HashMap<String,String> map = new HashMap<>();
        map.put("1","11");
        map.put("2","22");
        map.put("3","33");
        map.put("4","44");
        map.forEach((k,v)-> System.out.println("key:"+k+"||V:"+v));
        System.out.println("-------------分割线----------------");
        Hashtable hashtable = new Hashtable();
        hashtable.put("x","XXX");
        hashtable.put("y","YYY");
        hashtable.put("z","ZZZ");
        hashtable.forEach((k,v)-> System.out.println("key>>>>"+k+"||value>>>>>>>"+v));
        System.out.println("-------------分割线----------------");
        ArrayList list = new ArrayList();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.forEach(i-> System.out.println("list>>>>>"+i));
    }

    /**
     * map 这里map的作用是将一个对象变换为另外一个
     */
    public static void mapTest(){
        List<Double> cost = Arrays.asList(10.0, 20.0,30.0);
        cost.stream().map(x->x*0.5).forEach(y-> System.out.println("cost>>>>"+y));
    }

    /**
     * map转换并且输出线程名
     */
    public static void ThreadTest(){
        IntStream
                .range(0,5)
                .boxed()
                .map(
                        i->new Thread(()->System.out.println(Thread.currentThread().getName()))
                )
                .forEach(Thread::start);
    }

    /**
     * reduce实现的则是将所有值合并为一个
     */
    public static void mapReduceTest(){
        List<Double> cost = Arrays.asList(10.0, 20.0,30.0);
        double allCost = cost.stream().reduce((sum,x)->sum+x).get();
        System.out.println("allCost>>>>"+allCost);
        System.out.println("-------------分割线----------------");
        HashMap<String,Integer> map = new HashMap<>();
        map.put("1",11);
        map.put("2",22);
        map.put("3",33);
        map.put("4",44);
        int allCostFormap= map.values().stream().reduce((sum,k)->sum+k).get();
        System.out.println("allCostFormap>>>"+allCostFormap);
    }

    /**
     * filter原始集合中过滤掉一部分元素。数组和MAP
     */
    public static void filterTest(){
        List<Double> cost = Arrays.asList(10.0, 20.0,30.0);
        List<Double> filterCost = cost.stream().filter(x->x>15.0).collect(Collectors.toList());
        filterCost.forEach(x-> System.out.println(">>>>>"+x));
        System.out.println("-------------分割线----------------");
        HashMap<String,Integer> mapBefore = new HashMap<>();
        mapBefore.put("1",11);
        mapBefore.put("2",22);
        mapBefore.put("3",33);
        mapBefore.put("4",44);

        Map<String,Integer> filterMap = mapBefore
                .entrySet()
                .stream()
                .filter(map ->map.getValue() != 11)
                .collect(Collectors.toMap( p -> p.getKey(), p -> p.getValue()));
        System.out.println(">>>>>"+filterMap);

    }

    /**
     * 与函数式接口Predicate配合
     */
    public static void predicateTest(){
        List<String> languages = Arrays.asList("Java","Python","scala","Shell","R");
        System.out.println("Start With s :");
        filterTest(languages,x->x.startsWith("s"));
    }

    public static void filterTest(List<String> languages, Predicate<String> condition){
        languages.stream().filter(x->condition.test(x)).forEach(x-> System.out.println(">>>>>"+x));
    }
}
